package com.example.roundcornerprogressbar.slice;

import com.example.roundcornerprogressbar.ResourceTable;
import com.example.roundcornerprogressbar.view.progressbar.CenteredRoundCornerProgressBar;
import com.example.roundcornerprogressbar.view.progressbar.IconRoundCornerProgressBar;
import com.example.roundcornerprogressbar.view.progressbar.RoundCornerProgressBar;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.global.resource.ResourceManager;

public class SimpleAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_simple);
        initView();
    }

    private void initView() {

        RoundCornerProgressBar roundCornerProgressBar = (RoundCornerProgressBar)findComponentById(ResourceTable.Id_progressBarSimpleCustom);
        if (null != roundCornerProgressBar) {
            setProgressBarColor(roundCornerProgressBar);
        }
        roundCornerProgressBar.setBasePadding(4);
        roundCornerProgressBar.setMax(100);
        roundCornerProgressBar.setProgress(20);
        roundCornerProgressBar.setSecondaryProgress(30);
        roundCornerProgressBar.setRadius(30);

        RoundCornerProgressBar roundCornerProgressBar1 = (RoundCornerProgressBar)findComponentById(ResourceTable.Id_progressBarSimple1);
        if (null != roundCornerProgressBar) {
            setProgressBarColor(roundCornerProgressBar1);
        }
        roundCornerProgressBar1.setRadius(0);
        roundCornerProgressBar1.setProgress(50);

        RoundCornerProgressBar roundCornerProgressBar2 = (RoundCornerProgressBar)findComponentById(ResourceTable.Id_progressBarSimple2);
        if (null != roundCornerProgressBar) {
            setProgressBarColor(roundCornerProgressBar2);
        }
        roundCornerProgressBar2.setProgress(20);
        roundCornerProgressBar2.setSecondaryProgress(75);
        roundCornerProgressBar2.setRadius(80);
        roundCornerProgressBar2.setReverse(true);

        CenteredRoundCornerProgressBar centeredRoundCornerProgressBar = (CenteredRoundCornerProgressBar)findComponentById(ResourceTable.Id_progressBarCenteredCustom);
        if (null != centeredRoundCornerProgressBar) {
            setProgressBarColor(centeredRoundCornerProgressBar);
        }
        centeredRoundCornerProgressBar.setBasePadding(4);
        centeredRoundCornerProgressBar.setMax(100);
        centeredRoundCornerProgressBar.setProgress(20);
        centeredRoundCornerProgressBar.setSecondaryProgress(30);
        centeredRoundCornerProgressBar.setRadius(10);

        IconRoundCornerProgressBar iconRoundCornerProgressBar = (IconRoundCornerProgressBar)findComponentById(ResourceTable.Id_progressBarIconCustom);
        if (null != iconRoundCornerProgressBar) {
            setIconProgressBarColor(iconRoundCornerProgressBar);
        }
        iconRoundCornerProgressBar.setBasePadding(6);
        iconRoundCornerProgressBar.setIconPadding(5);
        iconRoundCornerProgressBar.setIconSize(80);
        iconRoundCornerProgressBar.setIconImageResource(ResourceTable.Media_ic_android);
        iconRoundCornerProgressBar.setMax(200);
        iconRoundCornerProgressBar.setRadius(8);
        iconRoundCornerProgressBar.setProgress(30);
        iconRoundCornerProgressBar.setSecondaryProgress(50);

        Button btDecrease = (Button)findComponentById(ResourceTable.Id_buttonDecrease);
        Button btAdd = (Button)findComponentById(ResourceTable.Id_buttonAdd);
        btDecrease.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                roundCornerProgressBar.setProgress(roundCornerProgressBar.getProgress()-20);
                roundCornerProgressBar.setSecondaryProgress(roundCornerProgressBar.getProgress()+10);

                iconRoundCornerProgressBar.setProgress(iconRoundCornerProgressBar.getProgress()-20);
                iconRoundCornerProgressBar.setSecondaryProgress(iconRoundCornerProgressBar.getProgress()+10);
            }
        });
        btAdd.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                roundCornerProgressBar.setProgress(roundCornerProgressBar.getProgress()+20);
                roundCornerProgressBar.setSecondaryProgress(roundCornerProgressBar.getProgress()+10);

                iconRoundCornerProgressBar.setProgress(iconRoundCornerProgressBar.getProgress()+20);
                iconRoundCornerProgressBar.setSecondaryProgress(iconRoundCornerProgressBar.getProgress()+10);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void setProgressBarColor(RoundCornerProgressBar roundCornerProgressBar) {
        ResourceManager resManager = getResourceManager();
        try {
            roundCornerProgressBar.setProgressBackgroundColor(resManager.getElement(ResourceTable.Color_sample_progress_background).getColor());
            roundCornerProgressBar.setSecondaryProgressColor(resManager.getElement(ResourceTable.Color_sample_progress_secondary).getColor());
            roundCornerProgressBar.setProgressColor(resManager.getElement(ResourceTable.Color_sample_progress_primary).getColor());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setIconProgressBarColor(IconRoundCornerProgressBar iconProgressBarColor) {
        ResourceManager resManager = getResourceManager();
        try {
            iconProgressBarColor.setProgressBackgroundColor(resManager.getElement(ResourceTable.Color_sample_progress_background).getColor());
            iconProgressBarColor.setSecondaryProgressColor(resManager.getElement(ResourceTable.Color_sample_progress_secondary).getColor());
            iconProgressBarColor.setProgressColor(resManager.getElement(ResourceTable.Color_sample_progress_primary).getColor());
            iconProgressBarColor.setIconBackgroundColor(resManager.getElement(ResourceTable.Color_sample_progress_icon_background).getColor());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
